package externalSort;

import java.io.File;

public class fileCleaner {

    /**
     * 清理指定目录下的所有文件
     * @param directoryPath 目录路径
     */
    public static void cleanDirectory(String directoryPath) {
        File directory = new File(directoryPath);
        if (!directory.exists()) {
            System.out.println("目录不存在: " + directoryPath);
            return;
        }

        File[] files = directory.listFiles();
        if (files == null) {
            return;
        }

        int deletedCount = 0;
        for (File file : files) {
            if (file.isFile()) {
                if (file.delete()) {
                    deletedCount++;
                } else {
                    System.out.println("无法删除文件: " + file.getName());
                }
            }
        }

        System.out.println("清理目录 " + directoryPath + " 完成，删除了 " + deletedCount + " 个文件");
    }

    /**
     * 清理多个目录
     * @param directories 目录路径数组
     */
    public static void cleanDirectories(String... directories) {
        for (String dir : directories) {
            cleanDirectory(dir);
        }
    }

    /**
     * 清理外部排序过程中生成的所有临时文件
     */
    public static void cleanAllExternalSortFiles() {
        String[] directories = {
                "./file/generation",
                "./file/merge",
                "./file/input",
                "./file/output",
                "output1.txt"
        };

        cleanDirectories(directories);
    }
}